{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### ARTEAR 2021.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '立体声混音 (Realtek HD Audio Stereo input)', 'hostapi': 4, 'max_input_channels': 2, 'max_output_channels': 0, 'default_low_input_latency': 0.01, 'default_low_output_latency': 0.01, 'default_high_input_latency': 0.04, 'default_high_output_latency': 0.04, 'default_samplerate': 48000.0}\n"
     ]
    }
   ],
   "source": [
    "import queue\n",
    "from pyqtgraph.Qt import QtGui, QtCore\n",
    "import numpy as np\n",
    "import pyqtgraph as pg\n",
    "from ssqueezepy import ssq_stft\n",
    "import sounddevice as sd\n",
    "from scipy.signal import resample\n",
    "\n",
    "app = pg.mkQApp(\"Plotting Example\")\n",
    "\n",
    "win = pg.GraphicsLayoutWidget(show=True, title=\"Basic plotting examples\")\n",
    "win.resize(1000,600)\n",
    "win.setWindowTitle('Plotting the system sounds')\n",
    "pg.setConfigOptions(antialias=True)\n",
    "\n",
    "device_id = 19\n",
    "channel = 1\n",
    "device_info = sd.query_devices(device_id, 'input')\n",
    "print(device_info)\n",
    "sample_rate = device_info['default_samplerate']\n",
    "\n",
    "p6 = win.addPlot(title=\"Updating plot\")\n",
    "curve = p6.plot(pen='y')\n",
    "ptr = 0\n",
    "\n",
    "q = queue.Queue()\n",
    "\n",
    "\n",
    "def audio_callback(indata, frames, time, status):\n",
    "    pass\n",
    "    #q.put(indata[::sample_rate, channel])\n",
    "\n",
    "\n",
    "def update():\n",
    "    global curve, q, ptr, p6\n",
    "    plot_data = q.get_nowait()\n",
    "    # plot_data = resample(q.get_nowait(), 1000)\n",
    "    #curve.setData(plot_data)\n",
    "    if ptr == 0:\n",
    "        p6.enableAutoRange('xy', False)  ## stop auto-scaling after the first data set is plotted\n",
    "    ptr += 1\n",
    "\n",
    "stream = sd.InputStream(\n",
    "    device=device_id, channels=channel,\n",
    "    samplerate=sample_rate, callback=audio_callback)\n",
    "\n",
    "timer = QtCore.QTimer()\n",
    "timer.timeout.connect(update)\n",
    "timer.start(50)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}